package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    @Override
    public void addBed(BedDto bedDto) {
        //将beddto的数据拷贝到新Bed对象中
        Bed bed = BeanUtil.toBean(bedDto, Bed.class);
        //设置对象的创建时间和修改时间
        bed.setCreateTime(LocalDateTime.now());
        bed.setUpdateTime(LocalDateTime.now());
        //设置床位的默认状态为0
        bed.setBedStatus(0);
        //设置床位创建者
        bed.setCreateBy(1L);
        //如果新增失败
        try {
            bedMapper.addBed(bed);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }
    }

    @Override
    public BedVo searchBed(Long id) {
        Bed bed = bedMapper.searchBed(id);
        BedVo bedVo = BeanUtil.toBean(bed, BedVo.class);
        return bedVo;
    }

    @Override
    public void updateBed(BedDto bedDto) {
        //将beddto的数据拷贝到新Bed对象中
        Bed bed = BeanUtil.toBean(bedDto, Bed.class);
        //刷新对象的修改时间
        bed.setUpdateTime(LocalDateTime.now());
        //设置床位更新者
        bed.setUpdateBy(1L);
        //状态码不为空，就更新
        if(ObjectUtil.isNotEmpty(bedDto.getBedStatus())){
            bed.setBedStatus(bedDto.getBedStatus());
        }
        //remark不为空，更新
        if(ObjectUtil.isNotEmpty(bedDto.getRemark())){
            bed.setRemark(bedDto.getRemark());
        }
        //更新
        bedMapper.updateBed(bed);
    }


    @Override
    public void delById(Long id) {
        bedMapper.delById(id);
    }
}

